<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>部门管理</title>
<link rel="stylesheet" href="../jquery-easyui-1.6.6/themes/default/easyui.css">
<link rel="stylesheet" href="../jquery-easyui-1.6.6/themes/icon.css">
<script type="text/javascript" src="../jquery-easyui-1.6.6/jquery.min.js"></script>
<script type="text/javascript" src="../jquery-easyui-1.6.6/jquery.easyui.min.js"></script>
<script type="text/javascript" src="../jquery-easyui-1.6.6/locale/easyui-lang-zh_CN.js"></script>

<script type="text/javascript">
	//加载datagrid
	$(function(){
		$("#dg").datagrid({
			checkbox:true,
			fit:true,
			fitColumns:true,
			rowNumbers:true,
			pagination:true,
			url:'../role/findAllRole.do',
			onLoadSuccess:function(){
				$("#dg").datagrid('expandAll');
			} 
		});
		$("#dg").pagination({
			pageSize:10,
		 	total:20,
		});
	});
	
	//搜索框
	function searchRole(){
		$("#dg").datagrid('load',{
			s_roleName:$("#s_roleName").val()
		});
	}
	//权限配置
	function accessConfig(){
		var rows=$("#dg").datagrid("getSelections");
		if(rows.length!=1){
			$.messager.alert("系统提示","请选择一条要修改权限的部门!");
			return;
		}
		var row=rows[0];
		roleId=row.roleId;
		$("#ddlg").dialog("open").dialog("setTitle","部门权限配置");
		url="../auth/authMenu.do?roleId="+row.roleId;
		$("#tree").tree({
			lines:true,
			url:url,
			checkbox:true,
			cascadeCheck:false,
			onLoadSuccess:function(){
				$("#tree").tree('expandAll');
			},
			onCheck:function(node,checked){
				if(checked){
					checkNode($("#tree").tree("getParent",node.target));
				}
			}
		});
	}
	
	function checkNode(node){
		if(!node){
			return;
		}else{
			checkNode($("#tree").tree("getParent",node.target));
			$("#tree").tree("check",node.target)
		}
	}
	
	//授权按钮
	function saveAuth(){
		var nodes=$("#tree").tree("getChecked");
		var authArrIds=[];
		for(var i=0;i<nodes.length;i++){
			authArrIds.push(nodes[i].id);
		}
		var authIds=authArrIds.join(",");
		$.messager.confirm("系统提示","您确定要对此部门进行授权吗？",function(r){
			if(r){
				$.post("../role/roleAuthIdsUpdate.do",{authIds:authIds,roleId:roleId},function(result){
					if(result.success){
						$.messager.alert("系统提示","恭喜您授权成功!");
						$("#ddlg").dialog("close");
					}else{
						$.messager.alert("系统提示",result.errorMsg);
					}
				},"json");
			}
		});
	}
</script>
</head>
<body>
	<table id="dg" title="部门管理" class="easyui-datagrid" toolbar="#toolbar">
		<thead>
			<tr>
				<th field="roleId" width="20px" align="center">编号</th>
				<th field="roleName" width="100px">名称</th>
				<th field="roleDescription" width="150px" align="center">备注</th>
			</tr>
		</thead>
	</table>
	<div id="toolbar" >
		<a href="javascript:openRoleAddDialog()" class="easyui-linkbutton" iconCls="icon-add" plain="true">新建</a>
		<a href="javascript:openRoleModifyDialog()" class="easyui-linkbutton" iconCls="icon-edit" plain="true">编辑</a>
		<a href="javascript:deleteRole()" class="easyui-linkbutton" iconCls="icon-remove" plain="true">删除</a>
		<a href="javascript:accessConfig()" class="easyui-linkbutton" iconCls="icon-permission" plain="true">权限配置</a>
		<div style="float:right">
			&nbsp;角色名称：&nbsp;<input type="text" name="s_roleName" id="s_roleName" size="20" onkeydown="if(event.keyCode==13) searchRole()"/>
			<a href="javascript:searchRole()" class="easyui-linkbutton" iconCls="icon-search" plain="true">搜索</a>
		</div>
	</div>
	<div id="ddlg" class="easyui-dialog" closed="true" buttons="#ddlg-buttons" style="width:300px;height:450px;padding:10px 20px;">
		<ul id="tree" class="easyui-tree"></ul>
	</div>
	
	<div id="ddlg-buttons">
		<a href="javascript:saveAuth()" class="easyui-linkbutton" iconCls="icon-ok" plain="true">授权</a>
		<a href="javascript:closeAuthDialog()" class="easyui-linkbutton" iconCls="icon-cancel" plain="true">关闭</a>
	</div>

</body>
</html>